package com.example.elasticsearch.controller;


import com.example.elasticsearch.entities.GoodsInfo;
import com.example.elasticsearch.esRepositor.GoodsRepository;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@RestController
public class GoodsController {

    @Autowired
    private GoodsRepository goodsRepository;

    @GetMapping("save")
    public String save(long id) {
        GoodsInfo goodsInfo = new GoodsInfo(id,
                "商品" + System.currentTimeMillis(), "这是"+id+"个测试商品");
        goodsRepository.save(goodsInfo);
        return "success";
    }

    @GetMapping("delete")
    public String delete(long id) {
        goodsRepository.deleteById(id);
        return "success";
    }

    @GetMapping("update")
    public String update(long id, String name, String description) {
        GoodsInfo goodsInfo = new GoodsInfo(id,
                name, description);
        goodsRepository.save(goodsInfo);
        return "success";
    }

    @GetMapping("getOne")
    public Optional<GoodsInfo> getOne(long id) {
        Optional<GoodsInfo> goodsInfoOptional = goodsRepository.findById(id);
        return  goodsInfoOptional;
    }
    @GetMapping("getAll")
    public Iterable<GoodsInfo> getAll() {
        Iterable<GoodsInfo> goodsInfos = goodsRepository.findAll();
        return  goodsInfos;
    }


//
//    @GetMapping("getForPage")
//    public void getForPage()
//        goodsRepository.findAll()
//
//    }
}
